package com.secxiot.consumer.user.controller;

import com.secxiot.common.entity.ResultVo;
import com.secxiot.common.utils.MapConvertUtil;
import com.secxiot.consumer.user.feign.UserClient;
import com.secxiot.consumer.user.service.DeptConsumerService;
import com.secxiot.provider.task.vo.SosDeptFacilityVo;
import com.secxiot.provider.user.dto.UserDeptDto;
import com.secxiot.provider.user.vo.DeptVo;
import com.secxiot.provider.user.vo.HospitalVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = {"部门相关"})
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private UserClient userClient;

    @Autowired
    private DeptConsumerService deptConsumerService;

    /**
     * 根据用户经纬度获取周边医院列表
     *
     * @param userId    用户id
     * @param longitude 用户经度
     * @param latitude  用户经度
     * @return 医院信息列表
     */
    @ApiOperation(value = "用户经纬度获取周边医院列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "longitude", value = "用户经度", required = true, paramType = "query", dataType = "Double"),
            @ApiImplicitParam(name = "latitude", value = "用户经度", required = true, paramType = "query", dataType = "Double"),
            @ApiImplicitParam(name = "wxAppId", value = "微信Id", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping(value = "hospitals")
    public ResultVo<List<HospitalVo>> hospitalListByUser(Long userId, BigDecimal longitude, BigDecimal latitude, String wxAppId) {
        List<HospitalVo> hospitalVos = userClient.listHospitalsByUser(userId, longitude, latitude, wxAppId);
        List<HospitalVo> list = hospitalVos.stream().map(detailVo -> {
            HospitalVo detail = new HospitalVo();
            BeanUtils.copyProperties(detailVo, detail);
            if (detailVo.getLongitude() != null && detailVo.getLatitude() != null) {
                Map<String, Double> map = MapConvertUtil.map_bd2tx(new Double(detailVo.getLatitude().toString()), new Double(detailVo.getLongitude().toString()));
                detail.setLatitude(new BigDecimal(map.get("lat").toString()));
                detail.setLongitude(new BigDecimal(map.get("lng").toString()));
            }
            return detail;
        }).collect(Collectors.toList());
        return new ResultVo<>(list);
    }


    /**
     * 获取所在医院SOS部门得设备
     *
     * @param userId 用户id
     * @return SOS部门设备列表
     */
    @ApiOperation(value = "获取所在医院SOS设备", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "long")
    })
    @GetMapping("sosFacility")
    public ResultVo<List<SosDeptFacilityVo>> sosFacility(Long userId) {
        // 限定医护人员
        //获取医护人员所在医院 和急诊科室
        List<UserDeptDto> userDeptDtos = userClient.listSupperDeptByUserId(userId);
        Long hospitalId = userDeptDtos.get(0).getDeptId();
        return new ResultVo<>(userClient.sosFacility(null, hospitalId));
    }


    @ApiOperation(value = "获取用户权限部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "form", dataType = "Long")
    })
    @GetMapping("/subs")
    public ResultVo<List<DeptVo>> getUserSimpleList(
            @RequestParam(value = "userId", required = true) Long userId
    ) {
        List<DeptVo> depts = deptConsumerService.getUserDeptSubs(userId);
        return new ResultVo<List<DeptVo>>(depts);
    }

    /**
     * 获取parentId下的部门列表
     *
     * @param parentId {0:获取医院列表}
     * @return
     */
    @ApiOperation(value = "获取parentId下的部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "部门parentId {0:获取医院列表}", required = true, paramType = "form", dataType = "Long")
    })
    @GetMapping("/list")
    public ResultVo<List<DeptVo>> listDeptByParentId(Long parentId) {
        List<DeptVo> depts = userClient.listDeptByParentId(parentId);
        return new ResultVo<List<DeptVo>>(depts);
    }

    @ApiOperation(value = "获取医疗机构详情--部门 Id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门Id", required = true, paramType = "form", dataType = "Long")
    })
    @GetMapping("/hospital/detailByDeptId/{deptId}")
    public ResultVo<HospitalVo> getHospitalByDeptId(@RequestParam(value = "deptId", required = true) Long deptId) {
        return new ResultVo<HospitalVo>(userClient.getHospitalByDeptId(deptId));
    }

    @ApiOperation(value = "获取医疗机构详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医疗机构Id", required = true, paramType = "form", dataType = "Long")
    })
    @GetMapping("/hospital/detail/{id}")
    public ResultVo<HospitalVo> getHospitalById(@RequestParam(value = "id", required = true) Long id) {
        return new ResultVo<HospitalVo>(userClient.getHospitalById(id));
    }

    @ApiOperation(value = "医疗机构列表")
    @GetMapping("/depts/hospital/list")
    public ResultVo<List<HospitalVo>> selectHospital() {
        return new ResultVo<List<HospitalVo>>(userClient.selectHospital());
    }

    /**
     * 获取医院详情
     *
     * @param longitude 经度
     * @param latitude  维度
     * @param deptId    部门Id
     * @return
     */
    @ApiOperation(value = "获取医院详情(经纬度)--部门Id", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门id", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "longitude", value = "用户经度", required = true, paramType = "query", dataType = "Double"),
            @ApiImplicitParam(name = "latitude", value = "用户经度", required = true, paramType = "query", dataType = "Double")
    })
    @GetMapping("/depts/hospital/getHospitalDistance/detail")
    public ResultVo<HospitalVo> getHospitalDistanceByDeptId(@RequestParam("longitude") BigDecimal longitude,
                                                            @RequestParam("latitude") BigDecimal latitude,
                                                            @RequestParam("deptId") Long deptId) {
        return new ResultVo<HospitalVo>(userClient.getHospitalDistanceByDeptId(longitude, latitude, deptId));
    }
}
